/*
 * contrib/intarray/_int_gin.c
 */
#include "postgres.h"

#include "_int.h"
#include "access/gin.h"
#include "access/stratnum.h"

PG_FUNCTION_INFO_V1(ginint4_queryextract);

Datum ginint4_queryextract(PG_FUNCTION_ARGS)
{
	int32	   *fc_nentries = (int32 *) PG_GETARG_POINTER(1);
	StrategyNumber fc_strategy = PG_GETARG_UINT16(2);
	int32	   *fc_searchMode = (int32 *) PG_GETARG_POINTER(6);
	Datum	   *fc_res = NULL;

	*fc_nentries = 0;

	if (fc_strategy == BooleanSearchStrategy)
	{
		QUERYTYPE  *fc_query = PG_GETARG_QUERYTYPE_P(0);
		ITEM	   *fc_items = GETQUERY(fc_query);
		int			fc_i;

		/* 空查询必须失败 */
		if (fc_query->size <= 0)
			PG_RETURN_POINTER(NULL);

		/*
		 * 如果查询没有任何所需的原始值（比如，像'! 42'），我们必须进行全索引扫描。
		 */
		if (query_has_required_values(fc_query))
			*fc_searchMode = GIN_SEARCH_MODE_DEFAULT;
		else
			*fc_searchMode = GIN_SEARCH_MODE_ALL;

		/*
		 * 提取所有的 VAL 项作为我们希望 GIN 检查的内容。
		 */
		fc_res = (Datum *) palloc(sizeof(Datum) * fc_query->size);
		*fc_nentries = 0;

		for (fc_i = 0; fc_i < fc_query->size; fc_i++)
		{
			if (fc_items[fc_i].type == VAL)
			{
				fc_res[*fc_nentries] = Int32GetDatum(fc_items[fc_i].val);
				(*fc_nentries)++;
			}
		}
	}
	else
	{
		ArrayType  *fc_query = PG_GETARG_ARRAYTYPE_P(0);

		CHECKARRVALID(fc_query);
		*fc_nentries = ARRNELEMS(fc_query);
		if (*fc_nentries > 0)
		{
			int32	   *fc_arr;
			int32		fc_i;

			fc_res = (Datum *) palloc(sizeof(Datum) * (*fc_nentries));

			fc_arr = ARRPTR(fc_query);
			for (fc_i = 0; fc_i < *fc_nentries; fc_i++)
				fc_res[fc_i] = Int32GetDatum(fc_arr[fc_i]);
		}

		switch (fc_strategy)
		{
			case RTOverlapStrategyNumber:
				*fc_searchMode = GIN_SEARCH_MODE_DEFAULT;
				break;
			case RTContainedByStrategyNumber:
			case RTOldContainedByStrategyNumber:
				/* 空集合包含在一切中 */
				*fc_searchMode = GIN_SEARCH_MODE_INCLUDE_EMPTY;
				break;
			case RTSameStrategyNumber:
				if (*fc_nentries > 0)
					*fc_searchMode = GIN_SEARCH_MODE_DEFAULT;
				else
					*fc_searchMode = GIN_SEARCH_MODE_INCLUDE_EMPTY;
				break;
			case RTContainsStrategyNumber:
			case RTOldContainsStrategyNumber:
				if (*fc_nentries > 0)
					*fc_searchMode = GIN_SEARCH_MODE_DEFAULT;
				else			/* 一切都包含空集合 */
					*fc_searchMode = GIN_SEARCH_MODE_ALL;
				break;
			default:
				elog(ERROR, "ginint4_queryextract: unknown strategy number: %d",
					 fc_strategy);
		}
	}

	PG_RETURN_POINTER(fc_res);
}

PG_FUNCTION_INFO_V1(ginint4_consistent);

Datum ginint4_consistent(PG_FUNCTION_ARGS)
{
	bool	   *fc_check = (bool *) PG_GETARG_POINTER(0);
	StrategyNumber fc_strategy = PG_GETARG_UINT16(1);
	int32		fc_nkeys = PG_GETARG_INT32(3);

	/* Pointer	   *extra_data = (Pointer *) PG_GETARG_POINTER(4); */
	bool	   *fc_recheck = (bool *) PG_GETARG_POINTER(5);
	bool		fc_res = false;
	int32		fc_i;

	switch (fc_strategy)
	{
		case RTOverlapStrategyNumber:
			/* 结果不是损失性 */
			*fc_recheck = false;
			/* check[] 中至少有一个元素为真，因此结果 = 真 */
			fc_res = true;
			break;
		case RTContainedByStrategyNumber:
		case RTOldContainedByStrategyNumber:
			/* 我们将需要重新检查 */
			*fc_recheck = true;
			/* check[] 中至少有一个元素为真，因此结果 = 真 */
			fc_res = true;
			break;
		case RTSameStrategyNumber:
			/* 我们将需要重新检查 */
			*fc_recheck = true;
			/* check[] 中必须所有元素为真 */
			fc_res = true;
			for (fc_i = 0; fc_i < fc_nkeys; fc_i++)
			{
				if (!fc_check[fc_i])
				{
					fc_res = false;
					break;
				}
			}
			break;
		case RTContainsStrategyNumber:
		case RTOldContainsStrategyNumber:
			/* 结果不是损失性 */
			*fc_recheck = false;
			/* check[] 中必须所有元素为真 */
			fc_res = true;
			for (fc_i = 0; fc_i < fc_nkeys; fc_i++)
			{
				if (!fc_check[fc_i])
				{
					fc_res = false;
					break;
				}
			}
			break;
		case BooleanSearchStrategy:
			{
				QUERYTYPE  *fc_query = PG_GETARG_QUERYTYPE_P(2);

				/* 结果不是损失性 */
				*fc_recheck = false;
				fc_res = gin_bool_consistent(fc_query, fc_check);
			}
			break;
		default:
			elog(ERROR, "ginint4_consistent: unknown strategy number: %d",
				 fc_strategy);
	}

	PG_RETURN_BOOL(fc_res);
}
